package com.party.service.study.impl;

import com.party.common.Page;
import com.party.mapper.CurriculumMapper;
import com.party.mapper.LearningContentMapper;
import com.party.model.*;
import com.party.service.study.CurriculumService;
import com.party.service.study.LearningContentService;
import com.party.service.study.User2ContentService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 011926 on 2014/9/23.
 */
@Repository
public class LearningContentServiceImpl implements LearningContentService
{
    @Resource
    LearningContentMapper learningContentMapper;
    @Resource
    User2ContentService user2ContentService;
    @Override
    public int add(LearningContent learningContent) {
        if(learningContent != null)
        {
            return this.learningContentMapper.insert(learningContent);
        }
        return 0;
    }

    @Override
    public int delete(Integer id) {
        if(id != null && id > 0)
        {
            return this.learningContentMapper.deleteByPrimaryKey(id);
        }
        return 0;
    }

    @Override
    public LearningContent getById(Integer id) {
        if(id != null && id > 0)
        {
            return this.learningContentMapper.selectByPrimaryKey(id);
        }
        return null;
    }

    @Override
    public List<LearningContent> getByIds(List<Integer> ids) {
        if(ids != null && ids.size() > 0)
        {
            LearningContentExample example = new LearningContentExample();
            LearningContentExample.Criteria criteria = example.createCriteria();
            criteria.andIdIn(ids);
            return this.learningContentMapper.selectByExample(example);
        }
        return null;
    }

    @Override
    public List<LearningContent> getByCurrId(Integer currId) {
        if(currId != null && currId > 0)
        {
            return this.learningContentMapper.getListByCurrId(currId);
        }
        return null;
    }

    @Override
    public List<LearningContent> getByCurrTitle(String title) {
        if(title != null && !"".equals(title))
        {
            return this.learningContentMapper.getListByCurrTitle(title);
        }
        return null;
    }

    @Override
    public Page getList(LearningContent learningContent, int pageSize, int pageNumber) {

        LearningContentExample example = new LearningContentExample();
        if(learningContent != null)
        {
            LearningContentExample.Criteria criteria = example.createCriteria();
            if(learningContent.getTitle() != null && !"".equals(learningContent.getTitle()))
            {
                criteria.andTitleLike("%"+learningContent.getTitle()+"%");
            }
//            if(learningContent.getContent() != null && learningContent.getContent().length > 0)
//            {
//                criteria.andContentLike("%"+learningContent.getContent()+"%");
//            }
            if(learningContent.getType() != null && !"".equals(learningContent.getType()))//内容类型
            {
                criteria.andTypeEqualTo(learningContent.getType());
            }
            if(learningContent.getRealname() != null && !"".equals(learningContent.getRealname()))
            {
                criteria.andRealnameLike("%" + learningContent.getRealname() + "%");
            }
            criteria.andIdIsNotNull();
            if(learningContent.getCurrTitle() != null && !"".equals(learningContent.getCurrTitle()))
            {
                example.setCurrTitle(learningContent.getCurrTitle());
            }


        }
        //获取总的记录数量
        int count = this.learningContentMapper.getListPageCount(example);
        //获取指定的页码数据
        example.setBeginNum((pageNumber - 1) * pageSize);
        example.setEndNum(pageSize);
        List<LearningContent> list = this.learningContentMapper.getListPage(example);
        //组合返回page
        Page page = new Page(pageNumber, count, pageSize, list);
        return page;

    }

    @Override
    public int edit(LearningContent learningContent) {
        if(learningContent != null &&learningContent.getId() != null && learningContent.getId() > 0)
        {
            return this.learningContentMapper.updateByPrimaryKeySelective(learningContent);
        }
        return 0;
    }

    @Override
    public List<LearningContent> getAll() {
        return this.learningContentMapper.selectByExample(new LearningContentExample());
    }

    @Override
    public List<LearningContent> getByUid(Integer uid) {
        if(uid != null && uid >0)
        {
            User2Content content = new User2Content();
            content.setUid(uid);
            List<User2Content> contents = user2ContentService.get(content);
            List<Integer> contentIds = new ArrayList<Integer>();
            for(User2Content uc:contents)
            {
                contentIds.add(uc.getLearningcontentid());
            }
            List<LearningContent> contentList = this.getByIds(contentIds);
            if(contentList != null && contentList.size() > 0)
            {
                for(LearningContent lc:contentList)
                {
                    for(User2Content uc:contents)
                    {
                        if(lc.getId() == uc.getLearningcontentid())
                        {
                            lc.setIsFinish(uc.getIsfinished());
                            lc.setRealtime(uc.getRealtime());
                            break;
                        }
                    }
                }
                return contentList;
            }
        }
        return null;
    }

    @Override
    public LearningContent getByIdUid(Integer contentId, Integer uid) {
        if(contentId != null && contentId >0 && uid !=null && uid > 0)
        {
            LearningContent learningContent = this.getById(contentId);
            User2Content uc = this.user2ContentService.get(uid,contentId);
            if(uc != null)
            {
                learningContent.setRealtime(uc.getRealtime());
                learningContent.setIsFinish(uc.getIsfinished());
            }
            return learningContent;
        }
        return null;
    }
}
